package com.zxlspace.collection;

/**
 * 基础map实现
 * map由键值对组成，键值不能重复
 */
public class SxtMap001 {

    private int size;

    private SxtEntry[] sxtEntries = new SxtEntry[1000];

    public int size() {
        return size;
    }

    public void put(Object key, Object value) {
        SxtEntry sxtEntry = new SxtEntry(key, value);
        //判断是否重复
        for (int i=0; i<size; i++) {
            if (sxtEntries[i].key.equals(key)) {
                sxtEntries[i].value = value;
                return;
            }
        }
        sxtEntries[size++] = sxtEntry;
    }

    public Object get(Object key) {
        Object value = null;
        for (int i=0; i<size; i++) {
            if (sxtEntries[i].key.equals(key)) {
                value = sxtEntries[i].value;
                return value;
            }
        }
        return value;
    }

    public boolean containsKey(Object key) {
        for (int i=0; i<size; i++) {
            if (sxtEntries[i].key.equals(key)) {
                return true;
            }
        }
        return false;
    }

    public void remove(Object key) {
        for (int i=0; i<size; i++) {
            if (sxtEntries[i].key.equals(key)) {
                int index = i;
                int numMoved = size - index;
                System.arraycopy(sxtEntries, index+1, sxtEntries, index, numMoved);
                size--;
            }
        }
    }

    public static void main(String[] args) {
        SxtMap001 map001 = new SxtMap001();
        map001.put("高崎", "张曼玉");
        map001.put("高崎1", "张曼玉1");
        map001.put("aaa", "vbbb");
        map001.put("ccc", "vbbb");

        Object object = map001.get("高崎1q");
        System.out.println(object);

        System.out.println( map001.containsKey("高崎3"));
        map001.remove("aaa");
        System.out.println(map001.size());

    }
}


class SxtEntry {
    Object key;

    Object value;

    public SxtEntry(Object key, Object value) {
        this.key = key;
        this.value = value;
    }
}
